<template>
  <!-- header部分 -->
    <header>
        <p>地址管理</p>
    </header>

  <!-- 地址列表部分 -->
    <ul class="addresslist">
                <li v-for="item in addressList" @click="setStoreDeliveryAddressId(item.id)">
                    <div class="addresslist-left">
                        <h3>{{item.contactName}} {{item.contactTel}}</h3>
                        <p>{{item.address}}</p>
                    </div>
                    <div class="addresslist-right">
                        <font-awesome-icon

                                @click.stop="router.push({path:`/editAddress/${item.id}`})"
                                :icon="['fas', 'pen-to-square']"
                        />
                        <font-awesome-icon :icon="['fas', 'trash']"  @click="deleteAddress(item.id)"/>
                    </div>
                </li>
    </ul>

  <!-- 新增地址部分 -->
    <div class="addbtn" @click="router.replace({path:'/addAddress'})">
        <font-awesome-icon :icon="['fas', 'circle-plus']" style="color: #0097FF"/>
        <p>新增收货地址</p>
    </div>
</template>

<script setup lang="ts">


import {onMounted, reactive, ref, toRaw} from "vue";
import {AddressVO} from "../type/address";
import {getAddressesAPI, removeDeliveryAddressAPI} from "../service/address.ts";
import {userStore} from "../store/user.ts";
import router from "../router";
const addressList = ref<AddressVO[]>([])
const user = userStore()
onMounted(async ()=>{
    //获取全部地址
    const res = await getAddressesAPI()
    if (res.data!=null){
        addressList.value = res.data
    }

})
const deleteAddress = async (id:number) =>{
    const res = await removeDeliveryAddressAPI(id)
    if (res.data != null && res.data){
        //移除
        addressList.value = addressList.value.filter((item)=>item.id!==id);
    }
}

const setStoreDeliveryAddressId = async (id:number)=>{
    await user.setDeliveryAddressId(id);
    router.back();
}

</script>

<style scoped>
/*************** header ***************/
header {
    width: 100%;
    height: 12vw;
    background-color: #0097FF;
    display: flex;
    justify-content: space-around;
    align-items: center;
    color: #fff;
    font-size: 4.8vw;
    position: fixed;
    left: 0;
    top: 0;
    /*保证在最上层*/
    z-index: 1000;
}
/*************** addresslist ***************/
.addresslist {
    width: 100%;
    margin-top: 12vw;
    background-color: #fff;
}
.addresslist li {
    width: 100%;
    box-sizing: border-box;
    border-bottom: solid 1px #DDD;
    padding: 3vw;
    display: flex;
}
.addresslist li .addresslist-left {
    flex: 5;
    /*左边这块区域是可以点击的*/
    user-select: none;
    cursor: pointer;
}
.addresslist li .addresslist-left h3 {
    font-size: 4.6vw;
    font-weight: 300;
    color: #666;
}
.addresslist li .addresslist-left p {
    font-size: 4vw;
    color: #666;
}
.addresslist li .addresslist-right {
    flex: 1;
    font-size: 5.6vw;
    color: #999;
    cursor: pointer;
    display: flex;
    justify-content: space-around;
    align-items: center;
}
/*************** 新增地址部分 ***************/
.addbtn {
    width: 100%;
    height: 14vw;
    border-top: solid 1px #DDD;
    border-bottom: solid 1px #DDD;
    background-color: #fff;
    margin-top: 4vw;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 4.5vw;
    color: #0097FF;
    user-select: none;
    cursor: pointer;
}
.addbtn p {
    margin-left: 2vw;
}

</style>
